﻿#include "http_manager.h"
#include <QUrl>
#include <QByteArray>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QEventLoop>
#include <QJsonObject>
#include <QJsonDocument>
#include <iostream>
using namespace std;

QByteArray http_manager::ssl_request_process(MOETH_TYPE moeth, QString url, QByteArray post_data)
{
    QByteArray result;

    try
    {
        QEventLoop eventLoop;
        QNetworkRequest request;

        QSslConfiguration conf = request.sslConfiguration();
        conf.setPeerVerifyMode(QSslSocket::VerifyNone);
        conf.setProtocol(QSsl::TlsV1SslV3);
        request.setSslConfiguration(conf);

        //request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        //request.setRawHeader("Referer", "https://wx2.qq.com/");
        //request.setHeader(QNetworkRequest::UserAgentHeader, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2652.0 Safari/537.36");

        if (moeth == MOETH_TYPE::POST)
        {
            request.setHeader(QNetworkRequest::ContentLengthHeader, post_data.length());
            request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
        }

        //qDebug() << "post url: " << url << endl;
        //qDebug() << "post_data: " << post_data << endl;

        QNetworkReply *reply = NULL;
        request.setUrl(QUrl(url));
        if (moeth == MOETH_TYPE::POST)
        {
            reply = m_manager.post(request, post_data);
        }
        else
        {
            reply = m_manager.get(request);
        }
        QObject::connect(&m_manager, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
        eventLoop.exec();

        result = reply->readAll();
        reply->deleteLater();
        m_manager.disconnect();
        m_manager.clearAccessCache();
        m_manager.clearConnectionCache();
    }
    catch (...)
    {
        return "";
    }

    return result;
}

QByteArray http_manager::ssl_request(MOETH_TYPE moeth, QString url, QByteArray post_data)
{
    int process_count = 0;

    while (true)
    {
        QByteArray result = ssl_request_process(moeth, url, post_data);
        if (result == "")
        {
            cout << " connect faild(" << ++process_count << "), reconnect ..." << endl;
            if (process_count > 1)
            {
                cout << " ssl_request exit ..." << endl;
                return "";
            }
        }
        else
        {
            return result;
        }
    }
}

QString http_manager::get_ip_info()
{
    return ssl_request(MOETH_TYPE::GET, "https://api.map.baidu.com/location/ip?ak=phGVPwnio24eni80HBWda1VoVyXqWSDy");
}

bool http_manager::send_err(QString sn, QString content)
{
    // 构建 Json 对象 - Page
    QJsonObject pageObject;
    pageObject.insert("SN", sn);
    //pageObject.insert("ip_info", get_ip_info());
    pageObject.insert("content", content);

    // 构建 Json 对象
    QJsonObject json;
    json.insert("BaseRequest", QJsonValue(pageObject));

    // 构建 Json 文档
    QJsonDocument document;
    document.setObject(json);
    QByteArray post_data = document.toJson(QJsonDocument::Compact);

    QByteArray result = ssl_request(MOETH_TYPE::POST, "http://localhost:3000/reg", post_data);
    if (result != "")
    {
        return true;
    }
    return false;
}